/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.core.view.global;

import com.intelligent.ispc.client.core.view.analysis.grr.GrrMainPanel;
import com.intelligent.ispc.client.core.view.analysis.report.YieldPanel;
import com.intelligent.ispc.client.core.view.analysis.spc.SpcMainPanel;
import com.intelligent.ispc.client.event.*;
import com.intelligent.ispc.client.utils.EventBusUtils;
import com.intelligent.ispc.client.utils.GBC;
import com.intelligent.ispc.client.utils.ResourceMessages;
import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.common.utils.ResourceBundleUtils;
import com.intelligent.ispc.common.utils.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by Peter on 2016/8/9.
 * <p>
 * Updated by Lucien on 2016/11/1
 */
@Component
public class MainTopToolBar extends JToolBar {

    private JToolBar currentBar;

    /**
     * Constructor
     */
    public MainTopToolBar() {
        this.currentBar = this;
    }

    /**
     * Initialize components
     */
    @PostConstruct
    public void initComponents() {
        this.setLayout(new GridBagLayout());

        btnSpc = new JButton();
        btnReport = new JButton();
        btnGrr = new JButton();
        btnConfig = new JButton();
        btnExport = new JButton();

        formatToolbarButton(btnSpc, "/icons/icon_bar.png", ResourceBundleUtils.getString(ResourceMessages.MENU_ANALYSIS_SPC));
        formatToolbarButton(btnReport, "/icons/icon_report.png", ResourceBundleUtils.getString(ResourceMessages.MENU_ANALYSIS_REPORT));
        formatToolbarButton(btnGrr, "/icons/icon_curve.png", ResourceBundleUtils.getString(ResourceMessages.MENU_ANALYSIS_GRR));
        formatToolbarButton(btnExport, "/icons/icon_price_watch.png", ResourceBundleUtils.getString(ResourceMessages.MENU_EXPORT));

        btnSpc.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_ANALYSIS_SPC));
        btnReport.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_ANALYSIS_REPORT));
        btnGrr.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_ANALYSIS_GRR));
        btnExport.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_EXPORT));

        btnSpc.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnSpcActionPerformed(e);
            }
        });

        btnReport.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnReportActionPerformed(e);
            }
        });

        btnGrr.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnGrrActionPerformed(e);
            }
        });

        btnExport.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnExportActionPerformed(e);
            }
        });

        this.add(btnSpc, new GBC(0, 0).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(0, 100));
        this.add(btnReport, new GBC(1, 0).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(0, 100));
        this.add(btnGrr, new GBC(2, 0).setFill(GBC.VERTICAL).setAnchor(GBC.WEST).setWeight(0, 100));
        this.add(new JPanel(), new GBC(3, 0).setFill(GBC.BOTH).setWeight(100, 100));
        this.add(btnExport, new GBC(4, 0).setFill(GBC.VERTICAL).setAnchor(GBC.EAST).setWeight(0, 100));

    }

    /**
     * Set export button is visible or not
     *
     * @param moduleKey which panel
     * @param isEnabled true or false
     */
    public void setExportBtnEnabled(String moduleKey, boolean isEnabled) {
        if (moduleKey == null) {
            this.moduleKey = null;
            btnExport.setVisible(false);
        } else {
            this.moduleKey = moduleKey;
            btnExport.setVisible(true);
            btnExport.setEnabled(isEnabled);
        }
    }

    private void btnSpcActionPerformed(ActionEvent evt) {
        try {
            EventBusUtils.post(new PanelChangeEvent(SpcMainPanel.class, true, ResourceMessages.MENU_ANALYSIS_SPC));
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/SPC."));
        } catch (ApplicationException ex) {
        }
    }

    private void btnReportActionPerformed(ActionEvent evt) {
        try {
            EventBusUtils.post(new PanelChangeEvent(YieldPanel.class, true, ResourceMessages.MENU_ANALYSIS_REPORT));
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/Report/Yield."));
        } catch (ApplicationException ex) {
        }
    }

    private void btnGrrActionPerformed(ActionEvent evt) {
        try {
//            Random r = new Random();
            EventBusUtils.post(new PanelChangeEvent(GrrMainPanel.class, true, ResourceMessages.MENU_ANALYSIS_GRR));
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
            EventBusUtils.post(new StatusBarProcessChangeEvent("Import Data", (int) (Math.random() * 100 + 1)));


        } catch (ApplicationException ex) {
        }
    }

    private void btnExportActionPerformed(ActionEvent evt) {
        try {
            EventBusUtils.post(new ExportBtnClickEvent(moduleKey, btnExport));
        } catch (ApplicationException ex) {
        }
    }

    private JButton formatToolbarButton(JButton btn, String iconPath, String tooltip) {
        if (StringUtils.isNotBlank(iconPath)) {
            ImageIcon icon = new ImageIcon(getClass().getResource(iconPath));
            btn.setIcon(icon);
        }

        btn.setBorderPainted(false);
        btn.setToolTipText(tooltip);
        btn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                btn.setBorderPainted(true);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                btn.setBorderPainted(false);
            }
        });
        return btn;
    }

    private JButton btnSpc;
    private JButton btnReport;
    private JButton btnGrr;
    private JButton btnExport;
    private String moduleKey;

    private JButton btnConfig;
}
